package com.ai.nsa.utils.lock;

import java.util.Map;

/**
 * @author Feng Chen
 */
public abstract class LockFactory {

    private LockFactory() {

    }

    public final static Integer CACHE_SIZE = 10000;

    private final static Map<Integer, Object> LOCKS = MapCache.newLRUCache(CACHE_SIZE, false);

    /**
     * 获取锁
     *
     * @param key 键
     * @return
     */
    public synchronized static Object getLock(String key) {

        Object lock;

        int hash = key.hashCode();

        if (!LOCKS.containsKey(hash)) {
            lock = new Object();
            LOCKS.put(hash, lock);
        } else {
            lock = LOCKS.get(hash);
        }

        return lock;
    }


}
